package it.unisa.ingegneria.tdp.esercitazione3;
import java.util.Iterator;

import org.junit.*;
import static org.junit.Assert.*;

public class BinarySearchTreeTest{
	
	private BinarySearchTree<Integer,String> bsTree;
	
	@Before
	public void setUp(){
		bsTree = new BinarySearchTree<Integer, String>();
	}
	
	
	@Test
	public void testInsert() throws InvalidKeyException{
		
		assertEquals(new Integer(5), bsTree.insert(5, "Info 1").getKey());
		assertEquals(new Integer(1), bsTree.insert(1, "Info 2").getKey());
		assertEquals("Info 3", bsTree.insert(2, "Info 3").getValue());
		assertNotEquals("Info 3", bsTree.insert(2, "Info 4").getValue());
		
	}
	
	
	@Test
	public void testFind() throws InvalidKeyException{
		
		bsTree.insert(5, "Info 1");
		bsTree.insert(1, "Info 2");
		bsTree.insert(2, "Info 3");
		
		assertEquals(new Integer(5), bsTree.find(5).getKey());
		assertNotEquals(new Integer(24), bsTree.find(5).getKey());
		assertNull(bsTree.find(25));
		
	}
	
	
	
	@Test(expected=InvalidEntryException.class)
	public void testRemove() throws InvalidEntryException, InvalidKeyException{
		
		bsTree.insert(5, "Info 1");
		bsTree.insert(2, "Info 2");
		bsTree.insert(1, "Info 3");
		bsTree.insert(10, "Info 4");
		
		assertEquals(new Integer(2), bsTree.remove(bsTree.find(2)).getKey());
		assertNull(bsTree.remove(bsTree.find(65)));
		assertNotNull(bsTree.find(1));
		
	}
	
	
	@Test
	public void testEntries() throws InvalidKeyException{
		
		bsTree.insert(5, "Info 1");
		bsTree.insert(2, "Info 2");
		bsTree.insert(1, "Info 3");
		bsTree.insert(10, "Info 4");
		
		Iterator<Entry<Integer, String>> it = bsTree.entries().iterator();
		
		assertEquals("Info 1", it.next().getValue());
		assertEquals(new Integer(2), it.next().getKey());
		assertEquals("Info 3", it.next().getValue());
		assertEquals(new Integer(10), it.next().getKey());

	}

}
